package com.zhiwen.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiwen.common.api.Result;
import com.zhiwen.domain.dto.QuestionDTO;
import com.zhiwen.domain.entity.Question;
import com.zhiwen.service.QuestionService;
import com.zhiwen.domain.vo.QuestionVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 问题控制器
 */
@Tag(name = "问题管理", description = "问题相关接口")
@RestController
@RequestMapping("/questions")
@RequiredArgsConstructor
public class QuestionController {

    private final QuestionService questionService;

    /**
     * 创建问题
     */
    @Operation(summary = "创建问题", description = "创建新的问题，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "成功",
                content = @Content(schema = @Schema(implementation = QuestionVO.class)))
    @PostMapping
    @SaCheckLogin
    public Result<QuestionVO> createQuestion(@RequestBody @Valid QuestionDTO questionDTO) {
        // 设置当前登录用户为问题创建者
        QuestionVO questionVO = questionService.createQuestion(questionDTO);
        return Result.success(questionVO);
    }

    /**
     * 更新问题
     */
    @Operation(summary = "更新问题", description = "更新已有问题，需要登录且只有创建者可以更新")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "成功",
                content = @Content(schema = @Schema(implementation = QuestionVO.class)))
    @PutMapping("/{id}")
    @SaCheckLogin
    public Result<QuestionVO> updateQuestion(
            @Parameter(description = "问题ID") @PathVariable Long id,
            @RequestBody @Valid QuestionDTO questionDTO) {
        // 检查是否是问题创建者
        questionDTO.setId(id);
        QuestionVO questionVO = questionService.updateQuestion(questionDTO);
        return Result.success(questionVO);
    }

    /**
     * 删除问题
     */
    @Operation(summary = "删除问题", description = "删除已有问题，需要登录且只有创建者可以删除")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "成功")
    @DeleteMapping("/{id}")
    @SaCheckLogin
    public Result<Void> deleteQuestion(@Parameter(description = "问题ID") @PathVariable Long id) {
        // 检查是否是问题创建者
        questionService.deleteQuestion(id);
        return Result.success();
    }

    /**
     * 获取问题详情
     */
    @Operation(summary = "获取问题详情", description = "根据ID获取问题详情，同时增加浏览量")
    @ApiResponse(responseCode = "200", description = "成功",
                content = @Content(schema = @Schema(implementation = QuestionVO.class)))
    @GetMapping("/{id}")
    public Result<QuestionVO> getQuestionDetail(@Parameter(description = "问题ID") @PathVariable Long id) {
        QuestionVO questionVO = questionService.getQuestionDetail(id);
        // 增加浏览量
        questionService.incrementViewCount(id);
        return Result.success(questionVO);
    }

    /**
     * 分页查询问题列表
     */
    @Operation(summary = "分页查询问题列表", description = "支持按分类、关键词和状态筛选")
    @Parameters({
        @Parameter(name = "categoryId", description = "分类ID"),
        @Parameter(name = "keyword", description = "搜索关键词"),
        @Parameter(name = "status", description = "问题状态"),
        @Parameter(name = "current", description = "当前页码", schema = @Schema(defaultValue = "1")),
        @Parameter(name = "size", description = "每页数量", schema = @Schema(defaultValue = "10"))
    })
    @ApiResponse(responseCode = "200", description = "成功")
    @GetMapping
    public Result<Page<QuestionVO>> getQuestionPage(
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Question> page = new Page<>(current, size);
        Page<QuestionVO> questionPage = questionService.getQuestionPage(categoryId, keyword, status, page);
        return Result.success(questionPage);
    }

    /**
     * 获取热门问题
     */
    @Operation(summary = "获取热门问题", description = "分页获取热门问题列表")
    @Parameters({
        @Parameter(name = "current", description = "当前页码", schema = @Schema(defaultValue = "1")),
        @Parameter(name = "size", description = "每页数量", schema = @Schema(defaultValue = "10"))
    })
    @ApiResponse(responseCode = "200", description = "成功")
    @GetMapping("/hot")
    public Result<Page<QuestionVO>> getHotQuestions(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Question> page = new Page<>(current, size);
        Page<QuestionVO> questionPage = questionService.getHotQuestions(page);
        return Result.success(questionPage);
    }

    /**
     * 获取等待回答的问题
     */
    @Operation(summary = "获取等待回答的问题", description = "分页获取等待回答的问题列表")
    @Parameters({
        @Parameter(name = "current", description = "当前页码", schema = @Schema(defaultValue = "1")),
        @Parameter(name = "size", description = "每页数量", schema = @Schema(defaultValue = "10"))
    })
    @ApiResponse(responseCode = "200", description = "成功")
    @GetMapping("/waiting")
    public Result<Page<QuestionVO>> getWaitingQuestions(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Question> page = new Page<>(current, size);
        Page<QuestionVO> questionPage = questionService.getWaitingQuestions(page);
        return Result.success(questionPage);
    }

    /**
     * 更新问题状态
     */
    @Operation(summary = "更新问题状态", description = "更新问题状态，需要登录且只有创建者可以更新")
    @SecurityRequirement(name = "Authorization")
    @Parameters({
        @Parameter(name = "id", description = "问题ID"),
        @Parameter(name = "status", description = "问题新状态")
    })
    @ApiResponse(responseCode = "200", description = "成功")
    @PutMapping("/{id}/status/{status}")
    @SaCheckLogin
    public Result<Void> updateQuestionStatus(
            @PathVariable Long id,
            @PathVariable Integer status) {
        // 检查操作权限
        questionService.updateQuestionStatus(id, status);
        return Result.success();
    }

    /**
     * 获取推荐问题
     */
    @Operation(summary = "获取推荐问题", description = "分页获取推荐问题列表")
    @Parameters({
        @Parameter(name = "current", description = "当前页码", schema = @Schema(defaultValue = "1")),
        @Parameter(name = "size", description = "每页数量", schema = @Schema(defaultValue = "10"))
    })
    @ApiResponse(responseCode = "200", description = "成功")
    @GetMapping("/recommend")
    public Result<Page<QuestionVO>> getRecommendQuestions(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Question> page = new Page<>(current, size);
        Page<QuestionVO> questionPage = questionService.getRecommendQuestions(page);
        return Result.success(questionPage);
    }

    /**
     * 获取关注用户的问题
     */
    @Operation(summary = "获取关注用户的问题", description = "分页获取已关注用户发布的问题，需要登录")
    @SecurityRequirement(name = "Authorization")
    @Parameters({
        @Parameter(name = "current", description = "当前页码", schema = @Schema(defaultValue = "1")),
        @Parameter(name = "size", description = "每页数量", schema = @Schema(defaultValue = "10"))
    })
    @ApiResponse(responseCode = "200", description = "成功")
    @GetMapping("/following")
    @SaCheckLogin
    public Result<Page<QuestionVO>> getFollowingUserQuestions(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Question> page = new Page<>(current, size);
        Page<QuestionVO> questionPage = questionService.getFollowingUserQuestions(page);
        return Result.success(questionPage);
    }
}
